/**
 * 
 */
package com.socialvampic.login;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.social.vampic.R;
import com.socialvampic.mvc.view.AbstractView;

/**
 * @author trungduy
 * 
 */
public class SignInView extends AbstractView {
	private Button mLoginButton;
	private Button mCancelButton;
	private EditText mAccountEditText;
	private EditText mPasswordEditText;
	private TextView mForgetPasswordTextView;
	private OnSignInListener mSignInListener;

	public SignInView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}

	public SignInView(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.socialvampic.mvc.view.AbstractView#createView(android.view.LayoutInflater
	 * , android.view.ViewGroup)
	 */
	@Override
	public void createView(LayoutInflater inflater, ViewGroup container) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.signin_screen, this);

		mLoginButton = (Button) view.findViewById(R.id.sign_in_ok_button);
		mCancelButton = (Button) view.findViewById(R.id.sign_in_cancel_button);
		mAccountEditText = (EditText) view
				.findViewById(R.id.sign_in_input_account_edittext);
		mPasswordEditText = (EditText) view
				.findViewById(R.id.sign_in_input_password_edittext);
		mForgetPasswordTextView = (TextView) view
				.findViewById(R.id.sign_in_forget_password_textview);
		mLoginButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (mSignInListener != null) {
					final String account = mAccountEditText.getEditableText()
							.toString();
					final String password = mPasswordEditText.getEditableText()
							.toString();
					mSignInListener.OnLogin(account, password);
				}
			}
		});

		mCancelButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (mSignInListener != null) {
					mSignInListener.OnCancel();
				}
			}
		});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.socialvampic.mvc.view.AbstractView#reloadView()
	 */
	@Override
	public void reloadView() {
		// TODO Auto-generated method stub
		mLoginButton.setText(R.string.sign_in_login_button);
		mCancelButton.setText(R.string.sign_in_cancel_button);
		mForgetPasswordTextView.setText(R.string.sign_in_forget_password);
		mAccountEditText.setHint(R.string.sign_in_input_account_hint);
		mPasswordEditText.setHint(R.string.sign_in_input_password_hint);
	}

	@Override
	public void destroyView() {
		// TODO Auto-generated method stub
		super.destroyView();
		removeAllViews();
		setOnSignInListener(null);
		mLoginButton.setOnClickListener(null);
		mCancelButton.setOnClickListener(null);
	}

	public void setOnSignInListener(OnSignInListener listener) {
		mSignInListener = listener;
	}

	/**
	 * Handle event on Sign In screen
	 * 
	 * @author trungduy
	 * 
	 */
	public static interface OnSignInListener {
		// cancel sign in view
		public void OnCancel();

		// login
		public void OnLogin(String account, String password);
	}

}
